package com.yang.dao;

import com.yang.entity.User;

import java.sql.*;
import java.util.*;

public class UserDao {
    //查询全部
    public List<User> queryUser() {
        Connection conn = BaseDao.getConn();
        PreparedStatement ps = null;
        ResultSet rs = null;
        try {
            ps = conn.prepareStatement("select * from User");
            rs = ps.executeQuery();
            List<User> list = new ArrayList<>();
            while (rs.next()) {
                User obj = new User();
                obj.setId(rs.getInt("id"));
                obj.setLogin(rs.getString("login"));
                obj.setName(rs.getString("name"));
                obj.setPwd(rs.getString("pwd"));
                obj.setSex(rs.getString("sex"));
                obj.setPhone(rs.getString("phone"));
                obj.setPhoto(rs.getString("photo"));
                obj.setBirthday(rs.getDate("birthday"));
                obj.setMaster(rs.getInt("master"));
                list.add(obj);
            }
            return list;
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            BaseDao.close(rs, ps, conn);
        }
        return null;
    }

    //根据用户名查询
    public User queryUserBylogin(String login) {
        Connection conn = BaseDao.getConn();
        PreparedStatement ps = null;
        ResultSet rs = null;
        try {
            ps = conn.prepareStatement("select * from User where login=?");
            ps.setString(1, login);
            rs = ps.executeQuery();
            if (rs.next()) {
                User obj = new User();
                obj.setId(rs.getInt("id"));
                obj.setLogin(rs.getString("login"));
                obj.setName(rs.getString("name"));
                obj.setPwd(rs.getString("pwd"));
                obj.setSex(rs.getString("sex"));
                obj.setPhone(rs.getString("phone"));
                obj.setPhoto(rs.getString("photo"));
                obj.setBirthday(rs.getDate("birthday"));
                obj.setMaster(rs.getInt("master"));
                return obj;
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            BaseDao.close(rs, ps, conn);
        }
        return null;
    }

    //根据用户id查询
    public User queryUserByid(int id) {
        Connection conn = BaseDao.getConn();
        PreparedStatement ps = null;
        ResultSet rs = null;
        try {
            ps = conn.prepareStatement("select * from User where id=?");
            ps.setInt(1, id);
            rs = ps.executeQuery();
            if (rs.next()) {
                User obj = new User();
                obj.setId(rs.getInt("id"));
                obj.setLogin(rs.getString("login"));
                obj.setName(rs.getString("name"));
                obj.setPwd(rs.getString("pwd"));
                obj.setSex(rs.getString("sex"));
                obj.setPhone(rs.getString("phone"));
                obj.setPhoto(rs.getString("photo"));
                obj.setBirthday(rs.getDate("birthday"));
                obj.setMaster(rs.getInt("master"));
                return obj;
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            BaseDao.close(rs, ps, conn);
        }
        return null;
    }
    //添加
    public int saveUser(User obj) {
        Connection conn = BaseDao.getConn();
        PreparedStatement ps = null;
        try {
            ps = conn.prepareStatement("insert into user(login, name, pwd, sex, phone, photo, birthday, master) values (?, ?, ?, ?, ?, ?, ?, ?)");
            ps.setString(1, obj.getLogin());
            ps.setString(2, obj.getName());
            ps.setString(3, obj.getPwd());
            ps.setString(4, obj.getSex());
            ps.setString(5, obj.getPhone());
            ps.setString(6, obj.getPhoto());
            ps.setDate(7, obj.getBirthday());
            ps.setInt(8, obj.getMaster());
            return ps.executeUpdate();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            BaseDao.close(null, ps, conn);
        }
        return 0;
    }

    //删除
    public int deleteUserById(Integer id) {
        Connection conn = BaseDao.getConn();
        PreparedStatement ps = null;
        try {
            ps = conn.prepareStatement("delete from User where id=?");
            ps.setInt(1, id);
            return ps.executeUpdate();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            BaseDao.close(null, ps, conn);
        }
        return 0;
    }

    //修改个人资料
    public int updateBySql(String sql) {
        Connection conn = BaseDao.getConn();
        PreparedStatement ps = null;
        try {
          ps=conn.prepareStatement(sql);
          return ps.executeUpdate();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            BaseDao.close(null, ps, conn);
        }
        return 0;
    }
}
